﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.CachTinhHocBongService;
namespace eTMS.Controllers{
public class CachTinhHocBongControllerBase : CommonController{
	public CachTinhHocBongControllerBase()
		: base()
	{	
		this.dao = new CachTinhHocBongServiceClient();
	}
	~CachTinhHocBongControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private CachTinhHocBongEntity oCachTinhHocBong;	
	public CachTinhHocBongEntity CachTinhHocBongObject{
		get{
			return oCachTinhHocBong;
		}
		set{
			oCachTinhHocBong=value;
		}
	}
	protected CachTinhHocBongServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSCachTinhHocBong(false);			
			return _Table;
		}
	}
	public void LoadAllDSCachTinhHocBong(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oCachTinhHocBong==null)return false;
		bool result=true;
		try{
			oCachTinhHocBong = dao.SaveRet(oCachTinhHocBong);
		}
		catch{
			result=false;
		}
		if(result){
			_ListCachTinhHocBong.Add(oCachTinhHocBong);
		}
		return result;
	}
	public bool Update(){
		if(oCachTinhHocBong==null)return false;
		return dao.Update(oCachTinhHocBong);
	}
	public bool Delete(){
		if(oCachTinhHocBong==null)return false;
		bool retvalue = dao.Delete(oCachTinhHocBong);
		if(retvalue){			
			_ListCachTinhHocBong.Remove(oCachTinhHocBong);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 CachTinhHocBong_ID){
		bool retvalue = dao.DeleteByID(CachTinhHocBong_ID);		
		if(retvalue){
			CachTinhHocBongEntity oDeleted=FindCachTinhHocBong(CachTinhHocBong_ID);
			if(oDeleted!=null){
				_ListCachTinhHocBong.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oCachTinhHocBong = new CachTinhHocBongEntity();
	}
    //Copy data from data row in grid to object
    public CachTinhHocBongEntity MapDataRowToObject(DataRow row){
		CachTinhHocBongEntity o = new CachTinhHocBongEntity();
		 o.TenCachTinh=Convert.ToString(row["TenCachTinh"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("CachTinh"))
			o.CachTinh=ConvertByte(row["CachTinh"]);
		 if(!row.IsNull("CachTinhHocBong_ID"))
		    o.CachTinhHocBong_ID=(Int32)(row["CachTinhHocBong_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(CachTinhHocBongEntity o,DataRow row){
		row["TenCachTinh"]=o.TenCachTinh;
		row["GhiChu"]=o.GhiChu;
		row["CachTinh"]=o.CachTinh;
		row["CachTinhHocBong_ID"]=o.CachTinhHocBong_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.CachTinhHocBongObject,row);
    }
	public void LoadObject(Int32 CachTinhHocBong_ID){
		CachTinhHocBongEntity oLoad=FindCachTinhHocBong(CachTinhHocBong_ID);
		if(oLoad==null){
			oCachTinhHocBong=this.LoadCachTinhHocBong(CachTinhHocBong_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oCachTinhHocBong.IsNullCachTinhHocBong_ID;
			if(!bNotIsNull){
				this.ListCachTinhHocBong.Add(oCachTinhHocBong);
			}
		}
		else{
			oCachTinhHocBong=oLoad;
		}
	}
	#region CachedList CachTinhHocBong
	private static List<CachTinhHocBongEntity> _ListCachTinhHocBong=new List<CachTinhHocBongEntity>();
    public List<CachTinhHocBongEntity>ListCachTinhHocBong{
		get{
			return _ListCachTinhHocBong;
		}
	}
	public static List<CachTinhHocBongEntity>ListCachTinhHocBongStatic{
		get{
			return _ListCachTinhHocBong;
		}
	}
	public CachTinhHocBongEntity FindCachTinhHocBong(Int32 CachTinhHocBong_ID){
		CachTinhHocBongEntity oResult=this.ListCachTinhHocBong.Find(delegate(CachTinhHocBongEntity oFind){
			bool result=true; 
			result&=oFind.CachTinhHocBong_ID.Equals(CachTinhHocBong_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllCachTinhHocBong(bool clearCache){
		if(_ListCachTinhHocBong.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		CachTinhHocBongServiceClient dao = new CachTinhHocBongServiceClient();
		_ListCachTinhHocBong=new List<CachTinhHocBongEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public CachTinhHocBongEntity LoadCachTinhHocBong(Int32 CachTinhHocBong_ID){			
		CachTinhHocBongEntity o=dao.SelectOne(CachTinhHocBong_ID);		
		return o;
	}
	#endregion
}
}
